package org.jetnuke.modules.admin.stories;

import org.apache.log4j.Logger;

import java.util.Date;
import java.util.GregorianCalendar;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.velocity.VelocityContext;
import org.jetnuke.common.ActionForward;
import org.jetnuke.common.IAction;
import org.jetnuke.common.IAdminInfo;
import org.jetnuke.common.IModuleInfo;
import org.jetnuke.common.IUserInfo;
import org.jetnuke.dao.DataAccessDriver;
import org.jetnuke.entities.Autonews;
import org.jetnuke.entities.Stories;
import org.jetnuke.util.JetUtil;

public class StoryPost implements IAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(StoryPost.class);

	int automated, acomm, ihome;

	int day, month, year, hour, minute;

	int cat, topic;

	String subject, story, name, story_ext, notes;
	String opr;
	int informantID;

	/**
	 * @param opr
	 *            The opr to set.
	 */
	public void setOpr(String opr) {
		this.opr = opr;
	}

	int qid = -1;

	public void setName(String name) {
		this.name = name;
	}

	public void setStory(String story) {
		this.story = story;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setCat(int cat) {
		this.cat = cat;
	}

	public void setTopic(int topic) {
		this.topic = topic;
	}

	public void setDay(int day) {
		this.day = day;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public void setAutomated(int automated) {
		this.automated = automated;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jetnuke.common.IAction#exec(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse,
	 *      org.apache.velocity.VelocityContext, org.jetnuke.common.IModuleInfo,
	 *      java.lang.String, org.jetnuke.common.IUserInfo,
	 *      org.jetnuke.common.IAdminInfo)
	 */
	public ActionForward exec(HttpServletRequest request,
			HttpServletResponse response, VelocityContext vc, IModuleInfo info,
			String op, IUserInfo ui, IAdminInfo ai) throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - start"); //$NON-NLS-1$
		}

		if (ai == null || ai.isSuperUser() == false)
			return null;
		if ("delete".equals(opr)) {
			DataAccessDriver.getInst().newQueueDAO().deleteByID(qid);
			JetUtil.redirect(vc, response, request.getContextPath()
					+ "/do/admin/submissions");

			if (logger.isDebugEnabled()) {
				logger
						.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
			}
			return null;
		}
		if (automated == 1) {// Program Story
			GregorianCalendar gc = new GregorianCalendar(year, month - 1, day,
					hour, minute, 0);
			Autonews news = new Autonews();

			news.setACOMM(acomm);
			news.setAID(ui.getID());
			news.setALANGUAGE("");
			news.setASSOCIATED("");
			news.setBODYTEXT(story_ext);
			news.setCATID(cat);
			news.setHOMETEXT(story);
			news.setIHOME(ihome);

			if (name != null && informantID > 1)
				news.setINFORMANT(informantID);
			else
				news.setINFORMANT(ui.getID());
			news.setNOTES(notes);
			news.setTIME(gc.getTime());
			news.setTITLE(subject);
			news.setTOPIC(topic);
			DataAccessDriver.getInst().newAutoNewsDAO().addNew(news);

			DataAccessDriver.getInst().newQueueDAO().deleteByID(qid);

			JetUtil.redirect(vc, response, request.getContextPath()
					+ "/do/admin");

		} else {
			Stories stor = new Stories();
			stor.setACOMM(acomm);
			stor.setAID(ui.getID());
			stor.setALANGUAGE("");
			stor.setASSOCIATED("");
			stor.setBODYTEXT(story_ext);
			stor.setCATID(cat);
			stor.setHOMETEXT(story);

			if (name != null && informantID > 1)
				stor.setINFORMANT(informantID);
			else
				stor.setINFORMANT(ui.getID());
			stor.setNOTES(notes);
			stor.setPOLLID(-1);
			stor.setTIME(new Date());
			stor.setTITLE(subject);
			stor.setTOPIC(topic);
			int storyID = DataAccessDriver.getInst().newStoriesDAO().addNew(
					stor);
			if (qid > -1)
				DataAccessDriver.getInst().newQueueDAO().deleteByID(qid);

			JetUtil.redirect(vc, response, request.getContextPath()
					+ "/do/news/story?id=" + storyID);

		}

		if (logger.isDebugEnabled()) {
			logger
					.debug("exec(HttpServletRequest, HttpServletResponse, VelocityContext, IModuleInfo, String, IUserInfo, IAdminInfo) - end"); //$NON-NLS-1$
		}
		return null;
	}

	public void setHour(int hour) {
		this.hour = hour;
	}

	public void setMinute(int minute) {
		this.minute = minute;
	}

	public void setAcomm(int acomm) {
		this.acomm = acomm;
	}

	public void setIhome(int ihome) {
		this.ihome = ihome;
	}

	public void setQid(int qid) {
		this.qid = qid;
	}

	/**
	 * @param story_ext
	 *            The story_ext to set.
	 */
	public void setStory_ext(String story_ext) {
		this.story_ext = story_ext;
	}

	/**
	 * @param notes
	 *            The notes to set.
	 */
	public void setNotes(String notes) {
		this.notes = notes;
	}

	public void setInformantID(int informantID) {
		this.informantID = informantID;
	}
}
